package com.kinglone.back.pdf.controller;

import com.kinglone.back.file.model.BasFile;
import com.kinglone.back.file.service.FileService;
import com.kinglone.common.poitwo.WordUtils;
import com.kinglone.common.utils.PropertieUtil;
import com.kinglone.common.utils.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.zip.ZipOutputStream;

@Controller
@RequestMapping("/uploadfile")
public class TestDemoController {

    @Autowired
    private FileService fileService;

    @RequestMapping("/createPdf")
    public void createPdf(HttpServletRequest request, HttpServletResponse response) {
        WordUtils wordUtil = new WordUtils();
        Map<String, Object> params = new HashMap<String, Object>();
       /* params.put("${position}", "java开发");
        params.put("${name}", "段然涛");
        params.put("${sex}", "男");
        params.put("${national}", "汉族");
        params.put("${birthday}", "生日");
        params.put("${address}", "许昌");
        params.put("${height}", "165cm");
        params.put("${biYeDate}", "1994-02-03");
        params.put("${landscape}", "团员");
        params.put("${zhuanYe}", "社会工作");
        params.put("${xueLi}", "本科");
        params.put("${school}", "江西科技师范大学");
        params.put("${phone}", "177");
        params.put("${eMail}", "157");*/

        try {
            Map<String, Object> header = new HashMap<String, Object>();
            header.put("width", 120);
            header.put("height", 120);
            header.put("type", "jpg");
            header.put("content", WordUtils.inputStream2ByteArray(new FileInputStream("D:\\file\\ewm.png"), true));
            params.put("${ppc}", header);
           /* Map<String, Object> header2 = new HashMap<String, Object>();
            header2.put("width", 120);
            header2.put("height", 120);
            header2.put("type", "jpg");
            header2.put("content", WordUtils.inputStream2ByteArray(new FileInputStream("D:\\file\\ewm.png"), true));
            params.put("${ppc}", header2);*/
            List<String[]> testList = new ArrayList<String[]>();
            testList.add(new String[]{"1", "1AA", "1BB", "1CC"});
            testList.add(new String[]{"2", "2AA", "2BB", "2CC"});
            testList.add(new String[]{"3", "3AA", "3BB", "3CC"});
            testList.add(new String[]{"4", "4AA", "4BB", "4CC"});
            String path = "D:\\file\\template\\test\\temp3.docx";  //模板文件位置
            String fileName = new String("测试文档.docx".getBytes("UTF-8"), "iso-8859-1");    //生成word文件的文件名
            wordUtil.getWord(path, params, testList, fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 打包压缩下载文件   123
     */
    @RequestMapping(value = "/downLoadZipFile")
    public void downLoadZipFile(HttpServletResponse response) throws IOException {
        String zipName = "myfile.zip";
        List<BasFile> fileList = fileService.getFileList();//查询数据库中记录
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition","attachment; filename="+zipName);
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        String basepath = PropertieUtil.getValue("config.properties", "upload_url");
       // String templatePath = basepath + File.separator + "template"+File.separator +"test"+File.separator+"testtemp.docx";
        try {
            for(Iterator<BasFile> it = fileList.iterator(); it.hasNext();){
                BasFile file = it.next();
                ZipUtils.doCompress(basepath+file.getReal_file_path(), out);
                response.flushBuffer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            out.close();
        }
    }

}
